package com.zxk.service.impl;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.ParamConstants;
import com.zxk.constant.PicConstants;
import com.zxk.enums.Coord;
import com.zxk.service.AbstractMyTask;
import com.zxk.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 运镖任务
 *
 * @author: 舟小亢
 * @date: 2023-08-31 22:05
 */
public class DartCarTaskImpl extends AbstractMyTask {
    private static Logger LOG = LoggerFactory.getLogger(DartCarTaskImpl.class);


    public DartCarTaskImpl(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    @Override
    public String getTaskName() throws Exception {
        return "运镖";
    }

    @Override
    public boolean execute() throws Exception {
        LOG.info("【运镖任务】开始！");

        // 找到活动并打开
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_活动)) {
            return false;
        }

        // 找到日常活动并点击
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_日常活动)) {
            return false;
        }

        // 找到运镖并参加
        MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, 1000);

        for (int i = 0; i < ParamConstants.UNIFY_TIMES; i++) {
            // 先判断是否已完成
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.运镖任务_完成图标, 2, 0.97);
            if (pic.isFinded()) {
                // 摁ESC，退出
                ThreadUtil.sleep1();
                KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
                LOG.error("【运镖任务】已完成！");
                return false;
            }
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.运镖任务_任务图标, 2);
            if (pic.isFinded()) {
                break;
            }
            // 如果没找到下拉滑动屏幕寻找图标
            MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, -100);

            // 一直没找到，可能任务卡住了
            if (i == ParamConstants.UNIFY_TIMES - 1) {
                LOG.error("【运镖任务】任务失败！");
                return false;
            }
        }
        MouseUtil.lClickRandom(hwnd_1,
                pic.getPicX() + Coord.ATTEND_TASK_DXY.getX(),
                pic.getPicY() + Coord.ATTEND_TASK_DXY.getY());

        // 点击接受
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.运镖任务_接受)) {
            return false;
        }

        //点击确认
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.运镖任务_确认)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkTask() throws Exception {
        LOG.info("【运镖任务】开始检查任务状态！");
        for (int i = 0; i < 20; i++) {
            // 确认状态：战斗中/运镖中
            pic = FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.运镖任务_战斗中, PicConstants.运镖任务_运镖中}, 3);
            if (pic.isFinded()) {
                LOG.info("【运镖任务】战斗中/运镖中！");
                ThreadUtil.sleep(30000);
                continue;
            }
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.运镖任务_接受, 3)) {
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.运镖任务_确认, 3);
            }
            // 如果找到长安成图标，没有找到接受图标，则说明运镖结束
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_长安城小图标, 3).isFinded()
                    && !FuzzyPicUtil.findPic(hwnd_0, PicConstants.运镖任务_接受, 3).isFinded()) {
                LOG.info("【运镖任务】任务已完成！");
                return true;
            }
        }
        LOG.info("【运镖任务】完成！");
        return true;
    }
}
